package com.alpha.realife.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @version V1.0
 * @author: Albert
 * @date: 2020/4/16 16:45
 * @description:
 */
public class CommonUtils {

    /**
     * {"maoYa":"3d73218db3514a7bbf2779f591db56341587022863723"}
     * 一个特殊场合下的cookie取值方案
     * 其他场合下不要乱用
     *
     * @param jsonString
     * @param index      0 用户名, 1 token
     * @return
     */
    public static String getUserCookieInfo(String jsonString, int index) {
        if (index < 0 || index > 1) {
            return "";
        }
        if (predicteStrNull(jsonString)) return "";
        //start
        JSONObject jsonObject = JSON.parseObject(jsonString);
        if (null == jsonObject) {
            return "";
        }

        //处理想不到的情况
        try {
            Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();

            Object[] strings = entries.toArray();
            Object string = strings[0];
            String string1 = string.toString();

            return string1.split("=")[index];
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 推断jsonString为空位null
     *
     * @param jsonString
     * @return
     */
    private static boolean predicteStrNull(String jsonString) {
        if (null == jsonString || "".equals(jsonString)) {
            return true;
        }
        return false;
    }


    /**
     * 造一个cookie
     *
     * @param cookieName
     * @param cookieValue
     * @param maxAge
     * @param domain
     * @return Cookie
     */
    public static Cookie createCookie(String cookieName, String cookieValue, int maxAge, String domain) {

        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setDomain(domain);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(maxAge);
        return cookie;
    }


    /**
     * 获取目标cookie
     *
     * @param request
     * @param targetCookieName
     * @return
     */
    public static Cookie getTargetCookie(HttpServletRequest request, String targetCookieName) {

        Cookie targetCookie = null;

        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                String cookieName = cookie.getName();
                if (null != cookieName && !"".equals(cookieName) && cookieName.equals(targetCookieName)) {
                    targetCookie = cookie;
                }
            }
        }

        return targetCookie;
    }


    /**
     * 造工具类
     *
     * @param jsonString
     * @return
     */
    public static Map<String, Object> jsonStringToMap(String jsonString) {

        if (predicteStrNull(jsonString)) return null;

        //start
        JSONObject jsonObject = JSON.parseObject(jsonString);
        if (null == jsonObject) {
            return null;
        }

        //处理想不到的情况
        try {
            Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();

            Map<String, Object> targetMap = new HashMap<>();
            Object[] objectsArr = entries.toArray();
            for (Object o : objectsArr) {

                if (null != o) {
                    String string = o.toString();
                    String indexVal0 = string.split("=")[0];
                    String indexVal1 = string.split("=")[1];
                    targetMap.put(indexVal0, indexVal1);
                }

            }

            if (null == targetMap) {
                return null;
            }
            return targetMap;
        } catch (Exception e) {

            return null;
        }
    }

}

